//
//  DXHomeBtnView.swift
//  新浪微博
//
//  Created by dix on 2017/2/6.
//  Copyright © 2017年 dix. All rights reserved.
//

import UIKit

class DXHomeBtnView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 设置视图
    private func setUpUI(){
        ///设置随机颜色
//        backgroundColor = DXRandomColor()
        //添加按钮
      let retweetBtn = addChilaBtn(imageName: "timeline_icon_retweet", title: "转发")
      let commentBtn = addChilaBtn(imageName: "timeline_icon_comment", title: "评论")
      let unlikeBtn = addChilaBtn(imageName: "timeline_icon_unlike", title: "赞")
        //添加竖线
        let l1 = addChildLine()
        let l2 = addChildLine()
        //添加约束
        retweetBtn.snp_makeConstraints { (make) in
            make.left.bottom.top.equalTo(self)
            make.width.equalTo(commentBtn)
        }
        commentBtn.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(retweetBtn.snp_right)
            make.width.equalTo(unlikeBtn)
        }
        unlikeBtn.snp_makeConstraints { (make) in
            make.top.right.bottom.equalTo(self)
            make.left.equalTo(commentBtn.snp_right)
        }
        l1.snp_makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetBtn.snp_right)
        }
        l2.snp_makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentBtn.snp_right) 
        }
    }

}

//MARK: - 抽取公共类方法
extension DXHomeBtnView{
    //创建按钮
    func addChilaBtn(imageName: String, title: String) -> UIButton {
        //实例化btn
        let btn = UIButton()
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), for: UIControlState.highlighted)
        //设置image
        btn.setImage(UIImage(named: imageName), for: UIControlState.normal)
        //设置title
        btn.setTitle(title, for: UIControlState.normal)
        //字体大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: DXNORMALFONTSIZE)
        //设置字体颜色
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        addSubview(btn)
        return btn
    }
    //创建竖线
    func addChildLine() -> UIImageView{
        let img = UIImageView(imgName: "timeline_card_bottom_line")
        //添加控件
        addSubview(img)
        return img
    }

}
